/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.activity.entity.VGroup;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.DelFlagEnum;
import com.ykht.proj.coupon.entity.FxCmsType;
import com.ykht.proj.coupon.service.FxCmsTypeService;
import com.ykht.proj.coupon.vo.FxCmsTypeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * 文章分类
 *
 * @author proj code generator
 * @date 2021-04-25 18:47:54
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxcmstype")
@Api(value = "fxcmstype", tags = "文章分类管理")
@Slf4j
public class FxCmsTypeController {

	private final FxCmsTypeService fxCmsTypeService;

	/**
	 * 分页查询
	 *
	 * @param page        分页对象
	 * @param fxCmsTypeVo 文章分类
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('coupon_fxcmstype_view')")
	public R getFxCmsTypePage(Page page, FxCmsTypeVo fxCmsTypeVo) {
		log.info("分页:{}+查询:{}", page, fxCmsTypeVo);
		QueryWrapper<FxCmsType> queryWrapper = new QueryWrapper<>();
		if (null != fxCmsTypeVo.getStatus()) queryWrapper.lambda().eq(FxCmsType::getStatus, fxCmsTypeVo.getStatus());
		if (null != fxCmsTypeVo.getName()) queryWrapper.lambda().like(FxCmsType::getName, fxCmsTypeVo.getName());
		if (null != fxCmsTypeVo.getCreateStaTime())
			queryWrapper.lambda().ge(FxCmsType::getCreateTime, fxCmsTypeVo.getCreateStaTime());
		if (null != fxCmsTypeVo.getCreateEndTime())
			queryWrapper.lambda().le(FxCmsType::getCreateTime, fxCmsTypeVo.getCreateEndTime());
		queryWrapper.lambda().orderByAsc(FxCmsType::getSort);
		return R.ok(fxCmsTypeService.page(page, queryWrapper));
	}


	/**
	 * 通过id查询文章分类
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxcmstype_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(fxCmsTypeService.getById(id));
	}

	/**
	 * 新增文章分类
	 *
	 * @param fxCmsType 文章分类
	 * @return R
	 */
	@ApiOperation(value = "新增文章分类", notes = "新增文章分类")
	@SysLog("新增文章分类")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('coupon_fxcmstype_add')")
	public R save(@RequestBody FxCmsType fxCmsType) {
		log.info("新增文章分类:{}", fxCmsType);
		FxCmsType sort = fxCmsTypeService.getOne(Wrappers.lambdaQuery(FxCmsType.class)
				.eq(FxCmsType::getSort, fxCmsType.getSort())
				.eq(FxCmsType::getDelFlag, DelFlagEnum.usable.getValue()));
		if (null != sort) {
			return R.failed("排序数字已存在！");
		}
		FxCmsType name = fxCmsTypeService.getOne(Wrappers.lambdaQuery(FxCmsType.class)
				.eq(FxCmsType::getName, fxCmsType.getName())
				.eq(FxCmsType::getDelFlag, DelFlagEnum.usable.getValue()));
		if (null != name) {
			return R.failed("类型名称已存在！");
		}
		fxCmsType.setCreateTime(LocalDateTime.now());
		fxCmsType.setCreateUser(SecurityUtils.getUserId());
		return R.ok(fxCmsTypeService.save(fxCmsType));
	}

	@ApiOperation(value = "校验 文章分类(类型名称, 排序数字)")
	@GetMapping("/checkFxCmsType")
	public R checkFxCmsType(FxCmsType fxCmsType) {
		log.info("->校验 文章分类(类型名称, 排序数字){}", fxCmsType);
		FxCmsType one = fxCmsTypeService.getOne(Wrappers.lambdaQuery(FxCmsType.class)
				.eq(!StringUtil.isEmpty(fxCmsType.getName()), FxCmsType::getName, fxCmsType.getName())
				.eq(null != fxCmsType.getSort(), FxCmsType::getSort, fxCmsType.getSort()));
		return R.ok(one == null ? true : one.getId().equals(one.getId()) ? true : false);
	}


	/**
	 * 修改文章分类
	 *
	 * @param fxCmsType 文章分类
	 * @return R
	 */
	@ApiOperation(value = "修改文章分类", notes = "修改文章分类")
	@SysLog("修改文章分类")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('coupon_fxcmstype_edit')")
	public R updateById(@RequestBody FxCmsType fxCmsType) {
		FxCmsType sort = fxCmsTypeService.getOne(Wrappers.lambdaQuery(FxCmsType.class)
				.ne(FxCmsType::getId, fxCmsType.getId())
				.eq(FxCmsType::getSort, fxCmsType.getSort())
				.eq(FxCmsType::getDelFlag, DelFlagEnum.usable.getValue()));
		if (null != sort) {
			return R.failed("排序数字已存在！");
		}
		FxCmsType name = fxCmsTypeService.getOne(Wrappers.lambdaQuery(FxCmsType.class)
				.ne(FxCmsType::getId, fxCmsType.getId())
				.eq(FxCmsType::getName, fxCmsType.getName())
				.eq(FxCmsType::getDelFlag, DelFlagEnum.usable.getValue()));
		if (null != name) {
			return R.failed("类型名称已存在！");
		}
		fxCmsType.setUpdateTime(LocalDateTime.now());
		fxCmsType.setUpdateUser(SecurityUtils.getUserId());
		return R.ok(fxCmsTypeService.updateById(fxCmsType));
	}


	/**
	 * 文章分类 上下架
	 *
	 * @param fxCmsType 文章分类 上下架
	 * @return R
	 */
	@ApiOperation(value = "文章分类 上下架", notes = "文章分类 上下架")
	@SysLog("文章分类 上下架")
	@PostMapping("updateStatus")
	public R updateStatus(@RequestBody FxCmsType fxCmsType) {
		log.info("文章分类 上下架:{}", fxCmsType);
		fxCmsType.setUpdateTime(LocalDateTime.now());
		fxCmsType.setUpdateUser(SecurityUtils.getUserId());
		return R.ok(fxCmsTypeService.updateById(fxCmsType));
	}


	/**
	 * 通过id删除文章分类
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除文章分类", notes = "通过id删除文章分类")
	@SysLog("通过id删除文章分类")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxcmstype_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(fxCmsTypeService.removeById(id));
	}

}
